const { DataTypes } = require("sequelize");
const sequelize = require("./db");
const { hash } = require("../utils/hash");

const Admin = sequelize.define("admin", {
  username: {
    type: DataTypes.STRING,
    allowNull: false,
    validate: {
      // 自定义验证规则 要求用户名在3到20个字符之间
      is: /^[a-zA-Z0-9_-]{3,20}$/,
    },
  },
  password: {
    type: DataTypes.STRING,
    allowNull: false,
    //添加设置器 给密码加密
    set(value) {
      this.setDataValue("password", hash(value));
    },
  },
});
module.exports = Admin;
